package test.fingers;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;

public class Fingers extends Activity {	
	private int numHumans, numComp, numCards;
	private SeekBar humanSeekBar, compSeekBar;
	private Dialog numPlayersDialog, numDealDialog;
	private boolean numPlayersOpen;
	
	// called when the app starts, load up main layout and wait till play is hit
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		numPlayersOpen = false;

		// this sets up a click listener for the play button
		findViewById(R.id.startPlayButton).setOnClickListener(
				new View.OnClickListener() {
					public void onClick(View v) {
						getNumPlayers();
					}
				});
	}
	
	private void getNumPlayers(){
		numPlayersDialog = new Dialog(Fingers.this);
		numPlayersDialog.setContentView(R.layout.gameinit);
		numPlayersDialog.setTitle("Number of Players?");
		numPlayersDialog.setCancelable(true);
		
		numPlayersOpen = true;
		
		numHumans = 1;
		numComp = 1;

		humanSeekBar = ((SeekBar) numPlayersDialog.findViewById(R.id.humanSeekBar));
		compSeekBar = ((SeekBar) numPlayersDialog.findViewById(R.id.compSeekBar));
		
		//dals with actions on the human seek bar
		humanSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
				numHumans = progress + 1;
				((TextView) numPlayersDialog.findViewById(R.id.numHumansText)).setText(Integer.toString(numHumans));
				//compSeekBar.setMax(5 - numHumans);
				if(numHumans + numComp > Gameplay.MAX_PLAYERS){
					numComp = Gameplay.MAX_PLAYERS - numHumans;
				}else if(numHumans == 1){
					numComp = 1;
				}else{
					return;
				}
				((TextView) numPlayersDialog.findViewById(R.id.numCompText)).setText(Integer.toString(numComp));
				compSeekBar.setProgress(numComp);
			}

			public void onStartTrackingTouch(SeekBar seekBar){}

			public void onStopTrackingTouch(SeekBar seekBar){}
		});

		//deals with actions o nthe computer seek bar
		compSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
			public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
				if(numHumans + progress > Gameplay.MAX_PLAYERS){
					numComp = Gameplay.MAX_PLAYERS - numHumans;
				}else if(numHumans == 1 && progress == 0){
					numComp = 1;
				}else{
					numComp = progress;
				}
				((TextView) numPlayersDialog.findViewById(R.id.numCompText)).setText(Integer.toString(numComp));
				compSeekBar.setProgress(numComp);
			}

			public void onStartTrackingTouch(SeekBar seekBar){}

			public void onStopTrackingTouch(SeekBar seekBar){}
		});
		
		
		// this sets up a click listener for the continue button
		numPlayersDialog.findViewById(R.id.gameInitContButton).setOnClickListener(
			new View.OnClickListener() {
				public void onClick(View v) {
					getNumDeal();
				}
			});
		
		numPlayersDialog.show();
	}
	
	private void getNumDeal(){
		numDealDialog = new Dialog(Fingers.this);
		numDealDialog.setContentView(R.layout.deallayout);
		numDealDialog.setTitle("Number of Cards to Deal?");
		numDealDialog.setCancelable(true);
		
		if(numPlayersOpen){ //closes the old dialog box
			numPlayersDialog.cancel();
			numPlayersOpen = false;
		}
		
		final EditText numDealBox = (EditText) numDealDialog.findViewById(R.id.cardsToDealTextBox);

		numDealBox.setOnKeyListener(new OnKeyListener(){
			public boolean onKey(View v, int keyCode, KeyEvent event){
				String str = numDealBox.getText().toString();
				if(str.equals(""))
					numCards = -1;
				else
					numCards = Integer.parseInt(str);

				// If the event is a key-down event on the "enter" button
				if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)){
					if(numCards >= Gameplay.MIN_HANDSIZE && numCards <= Gameplay.MAX_HANDSIZE){ 
						numDealDialog.cancel();
						goToBid();
						return true;
					}else{ //TODO add a notifcation/popup to say invalid input
						
					}
				}

				return false;
			}
		});

		// this sets up a click listener for the continue button
		numDealDialog.findViewById(R.id.dealContButton).setOnClickListener(new View.OnClickListener(){
			public void onClick(View v){
				if(numCards >= Gameplay.MIN_HANDSIZE && numCards <= Gameplay.MAX_HANDSIZE){ 
					numDealDialog.cancel();
					goToBid();
				}else{ //TODO add a notifcation/popup to say invalid input
					
				}
			}
		});
		
		numDealDialog.show();
	}
	
	private void goToBid() {
		sendGameInfo();
		Intent i = new Intent(this, Bid.class);
		startActivity(i);
	}
	
	private void sendGameInfo(){
		Gameplay.numHumans = numHumans;
		Gameplay.numComps = numComp;
		Gameplay.handSize = numCards;
	}
}